package team.you.change.util;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import team.you.change.dao.TeacherMapper;
import team.you.change.dao.UserMapper;
import team.you.change.pojo.Teacher;
import team.you.change.pojo.User;

import java.util.Random;
import java.util.UUID;

/**
 * 随机生成用户数据
 */
@RunWith(SpringRunner.class)
@ContextConfiguration({"classpath*:mapper/*.xml","classpath:spring-mybatis.xml"})
public class UserRandomCreate {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TeacherMapper teacherMapper;

    private static final Logger LOGGER = LogManager.getRootLogger();
    private static final String[] PHONE = {"159", "158", "136", "135", "147"};
    private static final String[] TEANO = {"117", "116", "115", "118"};

    @Test
//    @Transactional
    public void create() {
        for (int i = 0; i < 30; i++) {
            User user = new User();
            String uuid = UUID.randomUUID().toString();
            user.setId(uuid);
            user.setUsername(getName());
            user.setPassword(getPwd(user.getUsername()));
            user.setPhone(getPhone());
            String teaNo = getTeaNo();
            user.setTeaNo(teaNo);
            user.setUserType(1);
            //插入工号数据
            Teacher teacher = new Teacher();
            teacher.setTeacherNo(teaNo);
            teacher.setIsDel(0);
            int tea = teacherMapper.insert(teacher);
            if(tea > 0){
                int u = userMapper.insert(user);
                if(u > 0){
                    LOGGER.info("成功");
                }else LOGGER.info("用户插入失败");
            }else LOGGER.info("工号插入失败");
        }
    }

    /**
     * 随机生成教师工号
     * @return 工号
     */
    private String getTeaNo() {
        Random random = new Random();
        StringBuilder sb = new StringBuilder(TEANO[random.nextInt(TEANO.length)]);
        for (int i = 0; i < 8; i++) {
            sb.append(random.nextInt(10));
        }
        return sb.toString();
    }
    /**
     * 随机生成手机号
     * @return 手机号
     */
    private String getPhone() {
        Random random = new Random();
        StringBuilder sb = new StringBuilder(PHONE[random.nextInt(PHONE.length)]);
        for (int i = 0; i < 8; i++) {
            sb.append(random.nextInt(10));
        }
        return sb.toString();
    }

    /**
     * 随机生成一个名称
     * @return 名称
     */
    private String getName() {
        Random random = new Random();
        int i = random.nextInt(899) + 100;
        return "user" + i;
    }

    /**
     * 获取加密后密码， 密码固定‘123456pwd’
     *
     * @param name 用户名
     * @return 加密后字符串
     */
    private String getPwd(String name) {
        String pwd = "123456pwd";
        String md5 = Md5Util.encryption(name, pwd);
        return md5;
    }
}
